﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetFrame
{
    /// <summary>
    /// 将数据写入成二进制
    /// </summary>
    public class ByteArray
    {
        MemoryStream ms = new MemoryStream();
        BinaryWriter bw;
        BinaryReader br;

        public void Close()
        {
            bw.Close();
            br.Close();
            ms.Close();
        }

        public ByteArray(byte[] buff)
        {
            ms = new MemoryStream(buff);
            bw = new BinaryWriter(ms);
            br = new BinaryReader(ms);
        }
        /// <summary>
        /// 获取当前数据读取到的下标位置
        /// </summary>
        public int Position { get { return (int)ms.Position; } }
        /// <summary>
        /// 当前数据长度
        /// </summary>
        public int Length { get { return (int)ms.Length; } }
        /// <summary>
        /// 内存流中是否有消息体
        /// </summary>
        public bool Readnable { get { return ms.Length > ms.Position; } }
        /// <summary>
        /// 默认构造
        /// </summary>
        public ByteArray()
        {
            bw = new BinaryWriter(ms);
            br = new BinaryReader(ms);
        }

        public void WriteInt(int value)
        {
            bw.Write(value);
        }

        public void WriteByteArray(byte[] value)
        {
            bw.Write(value);
        }
        public void WriteByte(byte value)
        {
            bw.Write(value);
        }
        public void WriteBool(bool Value)
        {
            bw.Write(Value);
        }

        public void WriteString(string value)
        {
            bw.Write(value);
        }

        public void WriteDuoble(double value)
        {
            bw.Write(value);
        }

        public void WriteFloat(float value)
        {
            bw.Write(value);
        }

        public void WriteLong(long value)
        {
            bw.Write(value);
        }

        public int ReadInt()
        {
            return br.ReadInt32();
        }

        public byte[] ReadByteArray(int length)
        {
            return br.ReadBytes(length);
        }
        public byte ReadByte()
        { 
            return br.ReadByte();
        }

        public double ReadDouble()
        {
            return br.ReadDouble();
        }

        public float ReadFloat()
        {
            return br.ReadSingle();
        }

        public long ReadLong()
        {
            return br.ReadInt64();
        }

        public string ReadString()
        {
            return br.ReadString();
        }

        public bool ReadBool()
        {
            return br.ReadBoolean();
        }

        public void RePosition()
        {
            ms.Position = 0;
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public byte[] GetBuff()
        {
            byte[] result = new byte[ms.Length];
            Buffer.BlockCopy(ms.GetBuffer(), 0, result, 0, (int)ms.Length);
            return result;
        }
    }
}
